/*
 * Copyright (C) 2009 Dimitrios C. Michalakos
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.restsql.http;

import com.restsql.util.collection.MultiValuedMap;



/**
 * QueryString can be used for parsing querystrings.
 * @author dimitris@jmike.gr
 */
public class QueryString {

    private MultiValuedMap map = new MultiValuedMap();

    /**
     * Constructs a new QueryStringParser.
     * @param querystring the query string that is contained in the request URL after the path.
     */
    public QueryString(String querystring) {
        if (querystring != null) {//querystring is not null
            final String[] segments = querystring.split("&");//split querystring at '&'
            for (int i = 0; i < segments.length; i++) {//iterate segments
                String[] kv = segments[i].split("=", 2);////split segment at '='
                if (kv.length >= 2) {//kv contains key and value
                    map.add(kv[0], kv[1]);
                } else {//kv contains only name
                    map.add(kv[0], "");
                }
            }
        }
    }

    public MultiValuedMap getMultiValuedMap() {
        return map;
    }
}
